
const express = require("express");
const route = express.Router();

const advModel = require("../db/model/advModel")
const path = require("path")

const isLoginMiddle = require("../middleware/isLoginMiddle");



//查询广告 接口
route.get("/advList/:pageNO/:everyNo", isLoginMiddle, async (req, res) => {
  //分页 ???
  //pageNO 有几页
  //everyNo 每页几个
  const {
    pageNO,
    everyNo
  } = req.params;
  // console.log(pageNO);
  // console.log(everyNo);

  try {

    // const findResult = await advModel.find({}).limit(everyNo).skip(pageNO - 1) * 3;
    //获取请求的总数量
    const allAdvCount = await advModel.count({})
    //总页数
    // Math.ceil(allAdvCount/everyNo)
    //一页三个
    const findResult = await advModel.find({}).limit(everyNo).skip((pageNO - 1) * 3);

    res.send({
      code: 10000,
      message: "查询成功",
      data: {
        advList: findResult,
        pageCount :Math.ceil(allAdvCount/everyNo)
      }
    })
    // console.log(findResult);
  } catch (e) {
    console.log("查询广告列表异常" + e.message);
    res.send({
      code: 10001,
      message: "查询广告列表异常" + e.message,
      data: null
    })
  }

})


module.exports = route;